package com.gxc.utils;

import com.alibaba.fastjson.JSONObject;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;

/**
 * 日志输出工具类.
 *
 * @author 宫新程
 * @since 2018/12/15 5:26 PM
 */
@Slf4j
public class LogUtils {

  /** 输出日志. */
  public static void showInterceptorLog(HttpServletRequest request) {
    log.info("------  LoginInterceptor ------------------------------------");
    String param = JSONObject.toJSONString(request.getParameterMap());
    log.info("------  请求参数：{}", param);
    log.info("------  请求URL：{}", request.getRequestURL());
    log.info("------  请求Ip：{}", request.getRemoteHost());
    Map<String, String> headerMap = new HashMap<>();
    Enumeration<String> headerNames = request.getHeaderNames();
    while (headerNames != null && headerNames.hasMoreElements()) {
      String key = headerNames.nextElement();
      String header = request.getHeader(key);
      headerMap.put(key, header);
    }
    String headerJson = JSONObject.toJSONString(headerMap);
    log.info("------  请求Header：{}", headerJson);
    log.info("-------------------------------------------------------------");
  }

  /** 输出日志. */
  public static void showAspectLog(HttpServletRequest request) {
    log.info("------  MyAspect ------------------------------------");
    String param = JSONObject.toJSONString(request.getParameterMap());
    log.info("------  请求参数：{}", param);
    log.info("------  请求URL：{}", request.getRequestURL());
    log.info("------  请求Ip：{}", request.getRemoteHost());
    Map<String, String> headerMap = new HashMap<>();
    Enumeration<String> headerNames = request.getHeaderNames();
    while (headerNames != null && headerNames.hasMoreElements()) {
      String key = headerNames.nextElement();
      String header = request.getHeader(key);
      headerMap.put(key, header);
    }
    String headerJson = JSONObject.toJSONString(headerMap);
    log.info("------  请求Header：{}", headerJson);
    log.info("-------------------------------------------------------------");
  }
}
